/*
 * Filename:    CostCoverServiceImpl.java
 * Copyright:   2014 中国平安保险（集团）股份有限公司 版权所有
 * Company:     平安科技养老险及健康险系统开发部
 * Create at:   2014年9月29日
 */
package com.paic.mhis.hcpms.quota.settlement.biz.service.impl;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import com.paic.mhis.hcpms.quota.calculationreport.kpiinterface.biz.service.KpiCoreDataService;
import com.paic.mhis.hcpms.quota.calculationreport.kpiinterface.dto.KpiCateDTO;
import com.paic.mhis.hcpms.quota.calculationreport.kpiinterface.dto.KpiConditionDTO;
import com.paic.mhis.hcpms.quota.calculationreport.kpiinterface.dto.KpiGradeDTO;
import com.paic.mhis.hcpms.quota.calculationreport.kpiinterface.dto.KpiSociationDTO;
import com.paic.mhis.hcpms.quota.settlement.biz.service.CostConstansts;
import com.paic.mhis.hcpms.quota.settlement.biz.service.CostCoverService;
import com.paic.mhis.hcpms.quota.settlement.dto.CodeLabelDTO;
import com.paic.mhis.hcpms.quota.settlement.dto.CostCodeLabelDTO;
import com.paic.mhis.hcpms.quota.settlement.dto.CostDataDTO;
import com.paic.mhis.hcpms.quota.settlement.intergration.dao.ICodeLabelDAO;

@Component("costCoverService")
public class CostCoverServiceImpl implements CostCoverService {

	@Autowired
	private KpiCoreDataService kpiCoreDataService;
	@Autowired
	private ICodeLabelDAO codeLabelDao;
	@Autowired
	private CostCoverHospiatalLevelHelp hospitalLevelHelp;
	@Autowired
	private CostCoverSociationHelp sociationHelp;
	@Autowired
	private CostCoverHospiatalTypeHelp hospiatalTypeHelp;

	/**
	 * 费用分布中按等级和按类型的下拉列表值
	 */
	@Override
	public List<CostCodeLabelDTO> queryCostCodeLabelVO() {

		List<CodeLabelDTO> medicalType = this.codeLabelDao.queryCodeLabelDTOByType(CostConstansts.medicalOrgType);
		List<CodeLabelDTO> medicalLabel = this.codeLabelDao.queryCodeLabelDTOByType(CostConstansts.medicalOrgLabel);

		CostCodeLabelDTO vo = new CostCodeLabelDTO();
		vo.setType(CostConstansts.medicalOrgType);
		vo.setList(medicalType);

		CostCodeLabelDTO vo1 = new CostCodeLabelDTO();
		vo1.setType(CostConstansts.medicalOrgLabel);
		vo1.setList(medicalLabel);

		List<CostCodeLabelDTO> list = new ArrayList<CostCodeLabelDTO>();
		list.add(vo);
		list.add(vo1);

		return list;
	}

	/**
	 * 根据开始时间和结束时间得到得到日期位置map
	 * 
	 * @param startDate
	 * @param endDate
	 * @return
	 */
	public Map<String, Integer> createOrderList(String startDate, String endDate) {
		Map<String, Integer> map = new HashMap<String, Integer>();
		Calendar date = Calendar.getInstance();
		SimpleDateFormat sft = new SimpleDateFormat("yyyy-MM");
		int length = 0;
		try {
			if (StringUtils.hasText(startDate) && StringUtils.hasText(endDate)) {
				date.setTime(sft.parse(startDate));
				String data = "";
				if (sft.parse(startDate).getTime() <= sft.parse(endDate).getTime()) {
					do {
						data = sft.format(date.getTime());
						map.put(data, length);
						length++;
						date.add(Calendar.MONTH, 1);
					} while (!data.equals(endDate));
				}
			}
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return map;
	}

	/**
	 * 根据医院类型和医院级别以及门诊类型统计数据
	 */
	@Override
	public CostDataDTO queryCostDataDTOByParameter(String hospitalLev, String hospitalType, String visitType,
			String endDate) {
		CostDataDTO returnBean = null;

		List<String> kpiCodeList = CostConstansts.getKpiCodeList(); // 获取指标维度

		String[] date = endDate.split("-");
		// 当前月份向前推一年
		int year = Integer.parseInt(date[0]) - 1;
		int month = Integer.parseInt(date[1]) + 1;
		if (month > 12) {
			year = year + 1;
			month = 1;
		}
		String monthStr = "";
		if (month < 10) {
			monthStr = "0" + month;
		} else {
			monthStr = month + "";
		}
		String startTimeStr = year + "-" + monthStr;
		Map<String, Integer> orderMap = createOrderList(startTimeStr, endDate);

		if (CostConstansts.ALLDATA.equals(hospitalLev) || CostConstansts.ALLDATA.equals(hospitalType)) {

			KpiConditionDTO bean = new KpiConditionDTO();
			bean.setVisitType(visitType);
			bean.setKpiCodeList(kpiCodeList);
			bean.setKpiTimeStart(startTimeStr);
			bean.setKpiTimeEnd(endDate);

			List<KpiSociationDTO> sociationList = this.kpiCoreDataService.queryKpiBySoc(bean); // 统筹区查询
			returnBean = this.sociationHelp.getCostDataDTOFromSociation(sociationList, endDate, orderMap);
		}

		if (StringUtils.hasText(hospitalLev) && !CostConstansts.ALLDATA.equals(hospitalLev)) { // 医院级别

			KpiConditionDTO bean = new KpiConditionDTO();
			bean.setVisitType(visitType);
			bean.setKpiCodeList(kpiCodeList);
			bean.setKpiTimeStart(startTimeStr);
			bean.setKpiTimeEnd(endDate);

			bean.setGrade(hospitalLev);
			List<KpiGradeDTO> kpiGradeDTOList = this.kpiCoreDataService.queryKpiByGrade(bean); // 按照医院等级维度
			returnBean = this.hospitalLevelHelp.getCostDataDTOFromHospitalLevel(kpiGradeDTOList, endDate, orderMap);

		} else if (StringUtils.hasText(hospitalType) && !CostConstansts.ALLDATA.equals(hospitalType)) { // 医院类型

			KpiConditionDTO bean = new KpiConditionDTO();
			bean.setVisitType(visitType);
			bean.setKpiCodeList(kpiCodeList);
			bean.setKpiTimeStart(startTimeStr);
			bean.setKpiTimeEnd(endDate);

			bean.setCate(hospitalType);
			List<KpiCateDTO> kpiCateDtoList = this.kpiCoreDataService.queryKpiByCate(bean); // 按照医院类型维度查询

			returnBean = this.hospiatalTypeHelp.getCostDataDTOFromHospitalType(kpiCateDtoList, endDate, orderMap);

		}

		return returnBean;
	}

}
